#include "remotecontrolhandler.h"
#include "../thread/oseventthread.h"
#include "../event/vedioevent.h"
#include "../event/docevent.h"
#include "../event/senddocevent.h"
#include "../event/sendlocalevent.h"
#include "../event/sendsoundevent.h"
#include "../event/sendvedioevent.h"
#include "../event/showconnevent.h"
#include "../event/returnevent.h"
#include "../event/standbyevent.h"
#include "../event/controlmenuevent.h"
#include <QKeyEvent>
#include <QtDebug>
#include <QApplication>
#include <QPoint>
#include <QWidget>
#include <QDateTime>
#include <QElapsedTimer>

#define CONTROL_KEY_POWER   (59)
#define CONTROL_KEY_INDEX   (67)
#define CONTROL_KEY_RETURN  (87)
#define CONTROL_KEY_MORE    (68)
#define CONTROL_KEY_OK      (28)
#define CONTROL_KEY_VEDIO   (60)
#define CONTROL_KEY_DOC     (62)
#define CONTROL_KEY_F0      (61)
#define CONTROL_KEY_F1      (63)
#define CONTROL_KEY_F2      (64)
#define CONTROL_KEY_F3      (65)
#define CONTROL_KEY_F4      (66)
#define CONTROL_KEY_UP      (103)
#define CONTROL_KEY_DOWN    (108)
#define CONTROL_KEY_LEFT    (105)
#define CONTROL_KEY_RIGHT   (106)
#define CONTROL_KEY_DOT     (52)
#define CONTROL_KEY_DEL     (14)
#define CONTROL_KEY_0       (11)
#define CONTROL_KEY_1       (2)
#define CONTROL_KEY_2       (3)
#define CONTROL_KEY_3       (4)
#define CONTROL_KEY_4       (5)
#define CONTROL_KEY_5       (6)
#define CONTROL_KEY_6       (7)
#define CONTROL_KEY_7       (8)
#define CONTROL_KEY_8       (9)
#define CONTROL_KEY_9       (10)

#define NANO_SECONDS_ROBOT  (400)

RemoteControlHandler::RemoteControlHandler(QObject *parent) :
    QObject(parent),
    m_task(NULL)
{
    m_task = new OSEventThread;     //启动遥控器监听线程
    connect(m_task, SIGNAL(keySignal(int,int)), this, SLOT(processKey(int, int)));

    m_visibleStr.clear();

//    m_keyMapper.insert(CONTROL_KEY_0,Qt::Key_0);
//    m_keyMapper.insert(CONTROL_KEY_1,Qt::Key_1);
//    m_keyMapper.insert(CONTROL_KEY_2,Qt::Key_2);
//    m_keyMapper.insert(CONTROL_KEY_3,Qt::Key_3);
//    m_keyMapper.insert(CONTROL_KEY_4,Qt::Key_4);
//    m_keyMapper.insert(CONTROL_KEY_5,Qt::Key_5);
//    m_keyMapper.insert(CONTROL_KEY_6,Qt::Key_6);
//    m_keyMapper.insert(CONTROL_KEY_7,Qt::Key_7);
//    m_keyMapper.insert(CONTROL_KEY_8,Qt::Key_8);
//    m_keyMapper.insert(CONTROL_KEY_9,Qt::Key_9);
//    m_keyMapper.insert(CONTROL_KEY_DOT,Qt::Key_Period);
//    m_keyMapper.insert(CONTROL_KEY_DEL,Qt::Key_Backspace);
    m_keyMapper.insert(CONTROL_KEY_RETURN,Qt::Key_Escape);
//    m_keyMapper.insert(CONTROL_KEY_UP,Qt::Key_Up);
//    m_keyMapper.insert(CONTROL_KEY_DOWN,Qt::Key_Down);
//    m_keyMapper.insert(CONTROL_KEY_LEFT,Qt::Key_Left);
//    m_keyMapper.insert(CONTROL_KEY_RIGHT,Qt::Key_Right);

//    m_visibleStr.insert(CONTROL_KEY_0,"0");
//    m_visibleStr.insert(CONTROL_KEY_1,"1");
//    m_visibleStr.insert(CONTROL_KEY_2,"2");
//    m_visibleStr.insert(CONTROL_KEY_3,"3");
//    m_visibleStr.insert(CONTROL_KEY_4,"4");
//    m_visibleStr.insert(CONTROL_KEY_5,"5");
//    m_visibleStr.insert(CONTROL_KEY_6,"6");
//    m_visibleStr.insert(CONTROL_KEY_7,"7");
//    m_visibleStr.insert(CONTROL_KEY_8,"8");
//    m_visibleStr.insert(CONTROL_KEY_9,"9");
//    m_visibleStr.insert(CONTROL_KEY_DOT,".");
//    m_visibleStr.insert(CONTROL_KEY_DEL,"");
    m_visibleStr.insert(CONTROL_KEY_RETURN,"");
//    m_visibleStr.insert(CONTROL_KEY_UP,"");
//    m_visibleStr.insert(CONTROL_KEY_DOWN,"");
//    m_visibleStr.insert(CONTROL_KEY_LEFT,"");
//    m_visibleStr.insert(CONTROL_KEY_RIGHT,"");
}

void RemoteControlHandler::init()
{
    m_task->start();
}

RemoteControlHandler &RemoteControlHandler::instance()
{
    static RemoteControlHandler interface;
    return interface;
}

void RemoteControlHandler::quit()
{
    m_task->stop();
    m_task->quit();
    m_task->wait();
    delete m_task;
    m_task = NULL;
}

bool RemoteControlHandler::createEvent(int code, int way, QObject **r, QEvent **e)
{
    QWidget *object = QApplication::focusWidget();
    if (object == NULL)
    {
        return false;
    }
    QEvent *event = NULL;
    int keyway = way ? QEvent::KeyPress : QEvent::KeyRelease;

    if (CONTROL_KEY_OK == code)
    {   //点击ok
        int mouseway = way ? QEvent::MouseButtonPress : QEvent::MouseButtonRelease;
        QPoint p = object->mapToGlobal(object->pos());
        p = QPoint(p.x()+object->width()/2,p.y()+object->height()/2);
        event = new QMouseEvent((QEvent::Type)mouseway,
                                p,
                                Qt::LeftButton,
                                Qt::LeftButton,
                                Qt::NoModifier);
    } else if (CONTROL_KEY_POWER == code)
    {
        if (way == 0)
        {
            event = new StandbyEvent;     //待机
        }
    } else if (CONTROL_KEY_MORE == code)
    {
        if (way == 0)
        {
            event = new ControlMenuEvent;     //菜单
        }
    }
    else if (CONTROL_KEY_INDEX == code)
    {
        if (way == 0)
        {
            event = new ReturnEvent;     //返回
        }
    }
    else if (CONTROL_KEY_VEDIO == code)
    {
        if (way == 0)
            event = new VedioEvent;     //视频切换
    } else if (CONTROL_KEY_DOC == code)
    {
        if (way == 0)
            event = new DocEvent;       //文档切换

    } else if (CONTROL_KEY_F0 == code)
    {
        if (way == 0)
            event = new SendDocEvent;       //发送文档界面
    } else if (CONTROL_KEY_F1 == code)
    {
//        if (way == 0)
//            event = new SendSoundEvent;       //sound

    } else if (CONTROL_KEY_F2 == code)
    {
//        if (way == 0)
//            event = new SendVedioEvent;       //vedio

    } else if (CONTROL_KEY_F3 == code)
    {
//        if (way == 0)
//            event = new SendDocEvent;       //发送文档界面

    } else if (CONTROL_KEY_F4 == code)
    {
//        if (way == 0)
//            event = new ShowConnEvent;       //显示信息

    } else if (CONTROL_KEY_RETURN == code)
    {
        event = new QKeyEvent((QEvent::Type)keyway,
                              m_keyMapper.value(code),
                              Qt::NoModifier,m_visibleStr.value(code));
    }

    *r = object;
    *e = event;

    return ((event != NULL) && (object != NULL));
}

void RemoteControlHandler::processKey(int key, int way)
{
    static QElapsedTimer mstimer;             //这是为了防止键盘按一次发送多个键，做的过滤处理

    if (way == 0)
    {
        qint64 ns = mstimer.restart();
        qDebug() << "time:" << ns;
        if (ns != 0 && ns < NANO_SECONDS_ROBOT)
        {
            qDebug() << "drop this key:" <<key ;
            return;
        }
    }

    QObject *r = NULL;
    QEvent *e = NULL;
    bool rv = createEvent(key, way, &r, &e);
    qWarning() << "QObject:" << r << "," << "e:" << e;
    if (rv)
    {
        QApplication::postEvent(r,e);
    }
}
